// pages/report/report.js
import { areaList } from '@vant/area-data';
const app=getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stamp:440106,
    phone:"15876705385",
    currentDate:new Date().getTime(),
    minDate:new Date(2020, 11, 31).getTime(),
    maxDate: new Date(2099, 11, 31).getTime(),
    loading:false,
    // form:{
      name:"",
      phone:"",
      id:"",
      location:"",
      place:"",
      startTime:"",
      backTime:"",
      lastTime:"",
      traffic:"",
      trafficDetail:""
    // }
  },

  callPhone(){
    wx.makePhoneCall({
      phoneNumber: this.data.phone //仅为示例，并非真实的电话号码
    })
  },
  choosePlace(){
    this.animate('.area', [{
        height: 0
      },
      {
        height: "300px"
      }
    ], 110)
  },
  placeOut(){
    this.animate('.area', [{
      height: "300px"
    },
    {
      height: 0
    }
  ], 110)
  },
  placeComfirm(result){
    this.setData({
      ['place']:result.detail.values[0].name+'/'+result.detail.values[1].name+'/'+result.detail.values[2].name
    })
    this.placeOut()
  },
  chooseTime(e){  
    const Class=e.currentTarget.dataset.classname
    this.animate(Class, [{
      height: 0
    },
    {
      height: "300px"
    }
  ], 110)
  },
  timeComfirm(result){
    const time=result.currentTarget.dataset.time
    this.setData({
      [time]:`${new Date(result.detail.value).getFullYear()}/${new Date(result.detail.value).getMonth()+1}/${new Date(result.detail.value).getDate()}`
    })
    this.outTime()
  },
  outTime(){
    this.animate(".time", [
    {
      height: 0
    }
  ], 110)
  },
  submit(){

    for (const key in this.data) {
      if(this.data[key]===""){
        wx.showToast({
          title: '不能有未填项！',
          icon: 'error',
          duration: 4000
        })
        
        return
      }
      
      if(key==="phone"){
        if(this.data.phone.length<8||(!/^\d+$/.test(parseInt(this.data.phone)))){
          wx.showToast({
            title: '手机格式错误',
            icon: 'error',
            duration: 4000
          })
          return   
        }
      }
      if(key==="id"){
        if(this.data.id.length<14||(!/^\d+$/.test(parseInt(this.data.id)))){
          wx.showToast({
            title: '身份证号格式错误',
            icon: 'error',
            duration: 4000
          })
          return
        }
      }
    }
    this.setData({
      loading:true
    })
    db.collection('reportList').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        name:this.data.name,
        phone:this.data.phone,
        id:this.data.id,
        location:this.data.location,
        place:this.data.place,
        startTime:this.data.startTime,
        backTime:this.data.backTime,
        lastTime:this.data.lastTime,
        traffic:this.data.traffic,
        trafficDetail:this.data.trafficDetail,
        zhanshou:false
      },
      success: (res)=> {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        this.setData({
          loading:false
        })
        wx.showToast({
          title: '成功提交',
          icon: 'success',
          duration: 2000
        })
        setTimeout(()=>{
          wx.navigateBack({})
        },2000)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      areaList:areaList,
      headerTop:app.globalData.headerTop
    })
  },
  onShareAppMessage(){},
  onShareTimeline(){},
  onAddToFavorites(){},
  nothing(){}

  
})